_Pragma("once");

#include <vector>

#include "BasePage.h"
#include "MultiPage.h"
#include "LoopStatusListener.h"
#include "LoopManager.h"
#include "KeyCode.h"


/**
 * 回路设备分组控制
 * @author lizb
 */
class LoopStatusControlPage : public MultiPage<BasePage>, public LoopStatusListener {

public:
    /**
     * 默认LED数量 即默认回路按键数量
     */
    static constexpr char LOOP[] = "LOOP";
    static constexpr char AIR_SLAVE[] = "AIR_SLAVE";
    static constexpr char  DMX[] = "DMX";

    using super =  MultiPage<BasePage>;

    /**
     *
     * @param pageNum
     */
    LoopStatusControlPage(const int pageNum): super(pageNum), mloopManager(LoopManagerSingleton::instance()) {
        mloopManager.mpLoopStatusListener = this;
    }

    bool onLoopChanged(const size_t index, const LoopStatus status) override;
    bool onLoopChanged(LoopControlInfoPtr command) override;

    void beforeDisplay() override;
    void afterDisplay() override;


    /**
     * 初始化回路设备组 列表
     */
    void initialize() override {
        beforeDisplay();
    }

    void paint() override;


    int onChannelKeyPressed(const KeyCode& keyCode);

    bool displayLed(const std::string_view& loopStatusArray);

protected:

    std::string& clipStatusView(const std::string_view& statusArray, const LoopGroupInfo& loopGroup, const LoopSize_t needSize, std::string& clippedStatusArray) {
        const LoopSize_t minSize = std::min(needSize, loopGroup.portCount);

        if (statusArray.size() > loopGroup.firstIndexInLoopManager) {
            clippedStatusArray = statusArray.substr(loopGroup.firstIndexInLoopManager, minSize);
        }

            return clippedStatusArray;
    }

    void doSetPageNo(const int nextPageNo) override{
        super::doSetPageNo(nextPageNo);

        std::string loopStatus;
        mloopManager.getLoopStatus(loopStatus);
        displayLed(std::string_view(loopStatus));
    }


    LoopManager& mloopManager;

    /**
    * 回路组数组维护对象
    */
    LoopGroupInfos mloopGroups;

    std::vector<ExtendDeviceWeakPtr>* mpControllers;
};
